//
//  RootViewController.m
//  Demo2_UIImageView简易动画
//
//  Created by LZXuan on 15-5-6.
//  Copyright (c) 2015年 轩哥. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()
{
    UIImageView *_birdView;
    NSTimer *_timer;
}
@end

@implementation RootViewController
- (void)dealloc {
    [_birdView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatImageView];
    [self creatButton];
    
}
#pragma mark - 创建一个图片视图
- (void)creatImageView {
    //全屏背景
    UIImageView *backImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    backImageView.image = [UIImage imageNamed:@"back2.jpg"];
    [self.view addSubview:backImageView];
    [backImageView release];
    //先粘贴的在最下面
    
    _birdView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
    
    _birdView.image = [UIImage imageNamed:@"DOVE 1.png"];
    
    //给UIImageView设计一个动画数组
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for (NSInteger i = 1; i <= 18; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"DOVE %ld.png",i]];
        //放入数组
        [arr addObject:image];
    }
    //设置动画数组
    _birdView.animationImages = arr;
    [arr release];
    //设置动画的时间 周期 播放一组需要多久
    _birdView.animationDuration = 1;
    //播放的次数 0表示无限次
    _birdView.animationRepeatCount = 0;//
    
    [self.view addSubview:_birdView];
    
    //_birdView是一个成员变量 出现了alloc 只能在dealloc release

}
- (void)creatButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(200, 300, 100, 50);
    [button setTitle:@"开始" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    //设置圆角
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 8;
    
    
    button.backgroundColor = [UIColor grayColor];
    [self.view addSubview:button];
    
    
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerClick) userInfo:nil repeats:YES];
    //暂停
    [_timer setFireDate:[NSDate distantFuture]];
    
}
- (void)timerClick {
    //_birdView.frame.origin.x =1;
    //[_birdView frame].origin.x = 1;//错
    //getter 方法得到一个值  值是不能改得
    
    //1.先获取
    CGRect frame = _birdView.frame;
    if (frame.origin.x < [UIScreen mainScreen].bounds.size.width) {
        //右移
        frame.origin.x += 10;
    }else {
        frame.origin.x = -_birdView.frame.size.width;
    }
    //2.再赋给 bird
    _birdView.frame = frame;

}

- (void)btnClick:(UIButton *)button {
    //按钮控制定时器 和 动画的播放
    if (_birdView.isAnimating == YES) {
        //判断是否正在播放动画
        //暂停定时器  动画
        [_timer setFireDate:[NSDate distantFuture]];
        //停止动画
        [_birdView stopAnimating];
        [button setTitle:@"开始" forState:UIControlStateNormal];
    }else {
        [_timer setFireDate:[NSDate distantPast]];
        //开始动画
        [_birdView startAnimating];
        [button setTitle:@"暂停" forState:UIControlStateNormal];

    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
